package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.repository.GglUserRepository;
import net.starwifi.duobao.repository.GglhdRepository;
import net.starwifi.duobao.repository.GgljpRepository;
import net.starwifi.duobao.service.*;
import net.starwifi.duobao.utils.WinningUtils;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 微信活动
 * Created by Administrator on 2015/8/14.
 */
@Controller("apiGglhdController")
@RequestMapping("/gglhd")
public class GglActivityController extends BaseController {

    @Autowired
    MemberService memberService;

    @Autowired
    GglhdService gglhdService;

    @Autowired
    GglUserService gglUserService;

    @Autowired
    GglhdRepository gglhdRepository;

    @Autowired
    GgljpService ggljpService;

    @Autowired
    GgljpRepository ggljpRepository;

    @Autowired
    GglWinningService gglWinningService;

    @Autowired
    GglUserRepository gglUserRepository;

    @Autowired
    SettingService settingService;

    /**
     * 分享成功后扫码界面
     */
    @RequestMapping(value = "/gglOpen", method = RequestMethod.GET)
    public String gglOpen(Long gglhdid,ModelMap model) {
        String path ="";
        Gglhd gglhd = gglhdService.find(gglhdid);

        if(gglhd.getStatus()==0){
            gglhd.setStatus(3);//活动未开始
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cBegin = Calendar.getInstance();
        Calendar cEnd = Calendar.getInstance();
        Date startDate = DateTool.setHms(gglhd.getStartDate(), 0, 0, 0);
        Date endDate = DateTool.setHms(gglhd.getEndDate(), 23, 59, 59);
        cBegin.setTime(startDate);
        cEnd.setTime(endDate);
        if (!(cNow.after(cBegin))) {
            gglhd.setStatus(3);//活动未开始
        }
        if (!(cNow.before(cEnd))) {
            gglhd.setStatus(4);//活动已结束
        }
        model.addAttribute("gglhd", gglhd);
        if (gglhd.getType()==0){
            path="/gglhd/index";
        }
        if (gglhd.getType()==1){
            path="/dzphd/index";
        }
        return path;
    }

    /**
     * 开始抽奖
     */
    @RequestMapping(value = "/onStatus", method = RequestMethod.POST)
    @ResponseBody
    public Object onStatus(Long gglhdid, RedirectAttributes redirectAttributes) {
       Gglhd gglhd =gglhdService.find(gglhdid);
        //当前登录用户信息
        Member member = memberService.getCurrent();
        if (member.getId() == null) {
            return Message.error("0");
        }
        GglUser gglUser = new GglUser();
        //查询当前用户抽奖次数
        Long memberId = member.getId();
        int status = 0;
        int hdType = gglhd.getType();
        List<GglUser> gglUserList = gglUserRepository.ListUser(memberId, status,hdType);
        if (gglUserList.size() <= 0) {
            return Message.error("1");
        }
        //更新用户抽奖记录
        GglUser mapGglUser = gglUserList.get(0);
        mapGglUser.setStatus(1);
        gglUserService.update(mapGglUser);
        //用户剩余数
        int gglnumber = gglUserList.size();
        //开始抽奖
        Ggljp ggljp = new Ggljp();
        List<Ggljp> list = new ArrayList<Ggljp>();
        if (gglnumber > 0) {
            list = ggljpRepository.List(gglhdid);
            ggljp = WinningUtils.lottery(list);
        }
        if (ggljp != null) {
            //更新jiangp
            ggljpService.update(ggljp);

            status = 1;
            //保存中奖纪录
            GglWinning gglWinning = new GglWinning();
            gglWinning.setGglhdid(Long.toString(gglhd.getId()));
            gglWinning.setMobile(member.getMobile());
            gglWinning.setMemberId(memberId);
            gglWinning.setStatus(0);
            gglWinning.setGglhdname(gglhd.getName());
            gglWinning.setJp(ggljp.getJp());
            gglWinning.setJpname(ggljp.getName());
            gglWinning.setImgPath(ggljp.getShopPath());
            gglWinning.setWinningDate(new Date());
            gglWinning.setGglUserid(Long.toString(mapGglUser.getId()));
            gglWinningService.save(gglWinning);

        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gglnumber", gglnumber);
        map.put("ggljp", ggljp);


        return Message.createMessage(Message.success("成功"), map);
    }

    @RequestMapping(value = "/myGgl", method = RequestMethod.GET)
    public String myGgl(HttpServletRequest request, Model model) {
        Member member = memberService.getCurrent();
        List<net.starwifi.duobao.mvc.Order> orderBy = new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<GglWinning> winnings = gglWinningService.getListData(0,10,"o.memberId=?1", new Object[]{member.getId()}, orderBy);
        model.addAttribute("winnings", winnings);
        model.addAttribute("member", member);
        return "gglhd/myggl-list";
    }

    @RequestMapping(value = "/ggl-content", method = RequestMethod.GET)
    public String GglContent(Model model, int pageNum) {
        Member member = memberService.getCurrent();
        PageView pageView = new PageView(pageNum, 10);
        pageView.addOrder(net.starwifi.duobao.mvc.Order.desc("id"));
        pageView.addFilter(Filter.eq("memberId", member.getId()));
        Page<GglWinning> page = gglWinningService.getScrollData(pageView);
        model.addAttribute("winnings",page.getContent());
        return "gglhd/myggl-content";
    }


    /**
     * 刮刮乐活动列表
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/GglHd", method = RequestMethod.GET)
    public String GglHd(HttpServletRequest request, Model model) {
        List<net.starwifi.duobao.mvc.Order> orderBy = new ArrayList<net.starwifi.duobao.mvc.Order>();
        orderBy.add(net.starwifi.duobao.mvc.Order.desc("id"));
        List<Gglhd> gglhdList = gglhdService.getListData(0,10,"o.status=?1", new Object[]{1}, orderBy);
        model.addAttribute("gglhdList", gglhdList);
        return "gglhd/gglhd-list";
    }

    @RequestMapping(value = "/gglhd-content", method = RequestMethod.GET)
    public String GglHdContent(Model model, int pageNum) {
        PageView pageView = new PageView(pageNum, 10);
        pageView.addOrder(net.starwifi.duobao.mvc.Order.desc("id"));
        pageView.addFilter(Filter.eq("status", 1));
        Page<Gglhd> page = gglhdService.getScrollData(pageView);
        model.addAttribute("gglhdList",page.getContent());
        return "gglhd/gglhd-content";
    }

    /**
     * 分享成功后扫码界面
     */
    @RequestMapping(value = "/gglDetail", method = RequestMethod.GET)
    public String gglDetail(Long gglhdid,ModelMap model) {
        Gglhd gglhd = gglhdService.find(gglhdid);

        if(gglhd.getStatus()==0){
            gglhd.setStatus(3);//活动未开始
        }
        Calendar cNow = Calendar.getInstance();
        Calendar cBegin = Calendar.getInstance();
        Calendar cEnd = Calendar.getInstance();
        Date startDate = DateTool.setHms(gglhd.getStartDate(), 0, 0, 0);
        Date endDate = DateTool.setHms(gglhd.getEndDate(), 23, 59, 59);
        cBegin.setTime(startDate);
        cEnd.setTime(endDate);
        if (!(cNow.after(cBegin))) {
            gglhd.setStatus(3);//活动未开始
        }
        if (!(cNow.before(cEnd))) {
            gglhd.setStatus(4);//活动已结束
        }
        model.addAttribute("gglhd", gglhd);

        return "/gglhd/ggl_detail";
    }


}
